package com.wrl.mmall.controller.backend;

import com.wrl.mmall.common.ServerResponse;
import com.wrl.mmall.pojo.Product;
import com.wrl.mmall.service.IFileService;
import com.wrl.mmall.service.IProductService;
import com.wrl.mmall.util.PropertiesUtil;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 后端 商品管理
 * Created by Wang Renlei on 2018/9/20.
 */
@Slf4j
@Controller
@RequestMapping("/manage/product")
public class ProductManageController {

    @Autowired
    private IProductService iProductService;
    @Autowired
    private IFileService iFileService;

    /**
     * 添加或修改商品
     * 如果传入商品的id，既是修改；
     * 如果不传商品的id，既是添加
     *
     * @param product 商品的实体类
     * @return
     */
    @RequestMapping("save.do")
    @ResponseBody
    public ServerResponse productSave(Product product) {
        log.info("ProductManageController.productSave");
        return iProductService.saveOrUpdateProduct(product);
    }

    /**
     * 修改商品销售状态
     *
     * @param productId 商品id
     * @param status    需要修改的状态 0或1
     * @return
     */
    @RequestMapping("set_sale_status.do")
    @ResponseBody
    public ServerResponse setSaleStatus(Integer productId, Integer status) {
        log.info("ProductManageController.setSaleStatus");
        return iProductService.setSaleStatus(productId, status);
    }

    /**
     * 获取商品详情
     *
     * @param productId 商品id
     * @return
     */
    @RequestMapping("detail.do")
    @ResponseBody
    public ServerResponse getDetail(Integer productId) {
        log.info("ProductManageController.getDetail");
        return iProductService.manageProductDetail(productId);
    }

    /**
     * 查询所有商品
     *
     * @param pageNum  分页查询 页码
     * @param pageSize 分页查询 当前页数
     * @return
     */
    @RequestMapping("list.do")
    @ResponseBody
    public ServerResponse getList(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                  @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        log.info("ProductManageController.getList");
        return iProductService.getProductList(pageNum, pageSize);
    }

    /**
     * 模糊查询
     *
     * @param productName 模糊查询的商品名
     * @param productId   商品id
     * @param pageNum     分页查询 页码
     * @param pageSize    分页查询 当前页数
     * @return
     */
    @RequestMapping("search.do")
    @ResponseBody
    public ServerResponse productSearch(String productName,
                                        Integer productId,
                                        @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                        @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        log.info("ProductManageController.productSearch");
        return iProductService.searchProduct(productName, productId, pageNum, pageSize);
    }

    /**
     * 上传图片
     *
     * @param upload_file        springMVC的文件上传
     * @param httpServletRequest 通过request获取上下文，创建相对路径
     * @return
     */
    @RequestMapping("upload.do")
    @ResponseBody
    public ServerResponse upload(@RequestParam(value = "upload_file", required = false) MultipartFile upload_file,
                                 HttpServletRequest httpServletRequest) {
        log.info("ProductManageController.upload");
        //从session获取相对路径，会在WEB-INF下面创建upload文件夹
        String path = httpServletRequest.getSession().getServletContext().getRealPath("upload");
        String tragetFileName = iFileService.upload(upload_file, path);
        if (tragetFileName == null) {
            return ServerResponse.createByErrorMessage("图片上传失败");
        }
        String url = PropertiesUtil.getProperty("ftp.server.http.prefix") + tragetFileName;
        //组装返回值
        Map<String, String> fileMap = Maps.newHashMap();
        fileMap.put("uri", tragetFileName);
        fileMap.put("url", url);
        return ServerResponse.createBySuccess(fileMap);
    }

    /**
     * 富文本上传
     */
    @RequestMapping("richtext_img_upload.do")
    @ResponseBody
    public Map richtextImgUpload(@RequestParam(value = "upload_file", required = false) MultipartFile file,
                                 HttpServletRequest request,
                                 HttpServletResponse response) {
        log.info("ProductManageController.richtextImgUpload");
        Map<String, Object> resultMap = Maps.newHashMap();

        //富文本中对返回值有自己的要求，我们使用是simditor所以按照simditor的要求进行返回
        //simditor官方文档：https://simditor.tower.im/docs/doc-config.html
        //从session获取相对路径，会在WEB-INF下面创建upload文件夹
        String path = request.getSession().getServletContext().getRealPath("upload");
        String tragetFileName = iFileService.upload(file, path);
        if (StringUtils.isBlank(tragetFileName)) {
            resultMap.put("success", false);
            resultMap.put("msg", "上传失败");
            return resultMap;
        }
        String url = PropertiesUtil.getProperty("ftp.server.http.prefix") + tragetFileName;
        //组装返回值
        resultMap.put("success", true);
        resultMap.put("msg", "上传成功");
        resultMap.put("file_path", url);
        //跟前端的约定，设置返回头
        response.addHeader("Access-Control-Allow-Headers", "X-File-Name");
        return resultMap;
    }
}
